<?php

/* 通话记录 */

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class CountryController extends AdminBaseController
{

    public function index()
    {
        $data = $this->request->param();
        //print_r($data);exit;
        $map = [];
        $recommend = isset($data['recommend']) ? $data['recommend'] : '';
        if ($recommend != '') {
            $map[] = ['recommend', '=', $recommend];
        }


        $keyword = isset($data['keyword']) ? $data['keyword'] : '';
        if ($keyword != '') {
            $map[] = ['name|code', 'like', '%' . $keyword . '%'];
        }
        $nums = Db::name("country")->where($map)->where('type', 0)->count();
        $list = Db::name('country')
            ->where($map)
            ->where('type', 0)
            ->order('recommend desc')
            ->order('title asc')
            ->paginate(20);

        $list->appends($data);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('nums', $nums);
        return $this->fetch();
    }

    public function edit()
    {

        $id = $this->request->param('id', 0, 'intval');

        $data = Db::name('country')
            ->where("id={$id}")
            ->find();
        if (!$data) {
            $this->error("信息错误");
        }
        $this->assign('data', $data);
        return $this->fetch();
    }


    public function editPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();

            $id = $data['id'];
            $img = $data['img'];

            if ($img == '') {
                $this->error("请上传国旗");
            }

            $arr = array(
                'id' => $id,
                'name' => $data['name'],
                'code' => $data['code'],
                'title' => $data['title'],
                'recommend' => $data['recommend'],
                'unit' => $data['unit'],
                'mark' => $data['mark'],
                'cash_rate' => $data['cash_rate'],
                'cash_take' => $data['cash_take'],
                'cash_min' => $data['cash_min'],
                'name_str' => htmlspecialchars_decode(($data['name_str'])),
                'img' => $img,
                'name_cn' => $data['name_cn'],
            );
            //print_r($arr);exit;
            $rs = DB::name('country')->update($arr);

            if ($rs === false) {
                $this->error("保存失败！");
            }
            $this->success("保存成功！");
        }
    }


    public function language()
    {
        $data = $this->request->param();
        //print_r($data);exit;
        $map = [];
        $recommend = isset($data['recommend']) ? $data['recommend'] : '';
        if ($recommend != '') {
            $map[] = ['recommend', '=', $recommend];
        }


        $keyword = isset($data['keyword']) ? $data['keyword'] : '';
        if ($keyword != '') {
            $map[] = ['name|code', 'like', '%' . $keyword . '%'];
        }
        $nums = Db::name("country")->where($map)->where('type', 1)->count();
        $list = Db::name('country')
            ->where($map)
            ->where('type', 1)
            ->order('recommend desc')
            ->order('title asc')
            ->paginate(20);

        $list->appends($data);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->assign('nums', $nums);
        return $this->fetch();
    }

    public function editlang()
    {

        $id = $this->request->param('id', 0, 'intval');

        $data = Db::name('country')
            ->where("id={$id}")
            ->find();

        if (!$data) {
            $this->error("信息错误");
        }


        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editlangPost()
    {

        if ($this->request->isPost()) {
            $data = $this->request->param();

            $id = $data['id'];

            if ($data['is_default'] == 1) {
                // 修改语言的is_default = 0
                $where = [
                    ['type', '=', 1]
                ];
                DB::name('country')->where($where)->update(['is_default' => 0]);
            }

            $arr = array(
                'id' => $id,
                'name' => $data['name'],
                'code' => $data['code'],
                'title' => $data['title'],
                'recommend' => $data['recommend'],
                'name_str' => htmlspecialchars_decode(($data['name_str'])),
                'name_cn' => $data['name_cn'],
                'is_default' => $data['is_default'],
            );

            $rs = DB::name('country')->update($arr);

            if ($rs === false) {
                $this->error("保存失败！");
            }
            $this->success("保存成功！");
        }
    }

}